<?php
namespace App\Models;


use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class BaseModel extends Model
{

	protected $tableName;


	public function __construct($tables=''){
        
        if(!empty($tables))	$this->tableName=$tables;
        
    } 
    //查询所有
	public function getAll($where='',$select=''){
		$model=DB::table($this->tableName);
		if(!empty($select)) $model->select($select);
		if(!empty($where)) $model->where($where);
		return json_decode(json_encode($model->get()), true);
		//$arr=$model->get();
		//return $model->get()->toArray();
		//return array_map('get_object_vars', $arr);
	}
	//查询单条
	public function getOne($where='',$select=''){
		$model=DB::table($this->tableName);
		if(!empty($select)) $model->select($select);
		if(!empty($where)) $model->where($where);
		return json_decode(json_encode($model->first()), true);
	}
	//添加
	public function addArray($arr){
		return DB::table($this->tableName)->insert($arr);
	}

    public function addArrayByTableName($tn='', $arr) {
        $tname = $this->tableName;
        if(!empty($tn)) $tname=$tn;
        return DB::table($tname)->insert($arr);
    }

    public function addArrayGetId($arr){
        return DB::table($this->tableName)->insertGetId($arr);
    }

    public function updateOrInsertArray($tn='', $where, $arr){
        $tname = $this->tableName;
        if(!empty($tn)) $tname=$tn;
        return DB::table($tname)->updateOrInsert($where, $arr);
    }

	//修改
	public function editArray($where,$arr){
		return DB::table($this->tableName)->where($where)->update($arr);
	}
    public function editArrayByTableName($tn='', $where, $arr){
        $tname = $this->tableName;
        if(!empty($tn)) $tname=$tn;
        return DB::table($tname)->where($where)->update($arr);
    }
	//删除
	public function delArray($where){
		return DB::table($this->tableName)->where($where)->delete();
	}

    //删除 FK
    public function delFkArray($tableName, $where){
        return DB::table($tableName)->where($where)->delete();
    }

	//条件查询所有
    public function searchAll($where='',$select='',$searchlike='',$offset='',$limit=''){
        $model=DB::table($this->tableName);
        if(!empty($select)) $model->select($select);
        if(!empty($where)) $model->where($where);
        if(!empty($searchlike)){
            $model->where(function ($model) use ($searchlike) {
                //$model->where('name','like','%'.$searchlike.'%')->orWhere('artist','like','%'.$searchlike.'%');
                $model->where('name','like','%'.$searchlike.'%');
            });
        }
        $model->orderBy('updateTime','desc');
        if($limit) $model->offset($offset)->limit($limit);
        return json_decode(json_encode($model->get()), true);
    }

    //查询排序
    public function getAllOrderBy($where='',$select='',$orderby=''){
        $model=DB::table($this->tableName);
        if(!empty($select)) $model->select($select);
        if(!empty($where)) $model->where($where);
        if(!empty($orderby)) {
            foreach ($orderby as $k => $v) {
                $model->orderby($k, $v);
            }
        }
        return json_decode(json_encode($model->get()), true);
    }
    /**
    * orderby desc asc
    */
    //查询分页排序
    public function getAllOffsetOrderBy($where='',$select='',$offset='',$limit='',$orderby=''){
        $model=DB::table($this->tableName);
        if(!empty($select)) $model->select($select);
        if(!empty($where)) $model->where($where);
        if(!empty($orderby)) {
            foreach ($orderby as $k => $v) {
                $model->orderby($k, $v);
            }
        }
        if(!empty($limit)) $model->offset($offset)->limit($limit);
        return json_decode(json_encode($model->get()), true);
     }

}